package com.songlanyun.modules.pointconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.modules.pointconfig.entity.PointConfigLog;
import com.songlanyun.modules.pointconfig.model.dto.PointConfigDto;
import com.songlanyun.modules.pointconfig.service.PointConfigLogService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.pointconfig.dao.PointConfigDao;
import com.songlanyun.modules.pointconfig.entity.PointConfig;
import com.songlanyun.modules.pointconfig.service.PointConfigService;

import javax.annotation.Resource;


@Service("pointConfigService")
public class PointConfigServiceImpl extends ServiceImpl<PointConfigDao, PointConfig> implements PointConfigService {


    @Value("${pointconfig.platform-total-point}")
    private BigDecimal platformTotalPoint;

    @Value("${pointconfig.everyday-put-int-point}")
    private BigDecimal everyDayPutIntPoint;

    @Autowired
    private PointConfigLogService pointConfigLogService;

    @Override
    public PointConfig create(PointConfig entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public PointConfig modify(PointConfig entity) {
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Integer id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PointConfig> page = this.page(
                new Query<PointConfig>().getPage(params),
                new QueryWrapper<PointConfig>()
        );

        return new PageUtils(page);
    }

    @Override
    public void set(PointConfigDto dto) {
        PointConfig load =  this.loadConfig(true);
        if(ObjectUtil.isNull(load))
        {
            load = new PointConfig();
            BeanUtil.copyPropertiesIgnoreNull(dto,load);
            load.setPlatformTotalPoint(this.platformTotalPoint);
            load.setEveryDayPutIntPoint(this.everyDayPutIntPoint);
            this.create(load);
        }
        else
        {
            BeanUtil.copyPropertiesIgnoreNull(dto,load);
            load.setPlatformTotalPoint(this.platformTotalPoint);
            load.setEveryDayPutIntPoint(this.everyDayPutIntPoint);
            this.modify(load);
        }
        PointConfigLog entityLog = new PointConfigLog();
        entityLog.setActivationPeriodDay(load.getActivationPeriodDay());
        entityLog.setAdminUsername(load.getAdminUsername());
        entityLog.setEveryDayPutIntPoint(load.getEveryDayPutIntPoint());
        entityLog.setGiveAwayHandlingFeeRatio(load.getGiveAwayHandlingFeeRatio());
        entityLog.setPlatformTotalPoint(load.getPlatformTotalPoint());
        entityLog.setPointPrice(load.getPointPrice());
        entityLog.setRegisterGiveConsumption(load.getRegisterGiveConsumption());
        entityLog.setOpenPutIn(load.getOpenPutIn());
        pointConfigLogService.create(entityLog);
    }

    @Override
    public PointConfig loadConfig(Boolean t) {
        PointConfig config =  this.baseMapper.selectOne(new LambdaQueryWrapper<PointConfig>().last("limit 1"));
        if(ObjectUtil.isNotNull(config) || t)
        {
            return config;
        }

        return new PointConfig(){{
            setPlatformTotalPoint(platformTotalPoint);
            setEveryDayPutIntPoint(everyDayPutIntPoint);
        }};
    }

}
